﻿//by  张海军 710605420@qq.com
using System.IO;


namespace LazyFramework
{
    public class FileTool
    {
        private static long GB_SIZE = 1024 * 1024 * 1024;
        private static long MB_SIZE = 1024 * 1024;
        private static long KB_SIZE = 1024;
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="filePath">需要创建的目录路径</param>
        public static void CreateDirectory(string filePath)
        {
            if (!string.IsNullOrEmpty(filePath))
            {
                string dirName = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(dirName))
                {
                    Directory.CreateDirectory(dirName);
                }
            }
        }

        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="bytes">文件内容</param>
        public static void CreatFile(string filePath, byte[] bytes)
        {
            FileInfo file = new FileInfo(filePath);
            Stream stream = file.Create();

            stream.Write(bytes, 0, bytes.Length);

            stream.Close();
            stream.Flush();
            stream.Dispose();
        }
        /// <summary>
        /// 计算文件大小
        /// </summary>
        /// <returns></returns>

        public static string GetSize(long fileLength )
        {

            if (fileLength >= GB_SIZE)
            {
                return string.Format("{0:0.00}GB", fileLength * 1f / GB_SIZE);
            }
            else if (fileLength >= MB_SIZE)
            {
                return string.Format("{0:0.00}MB", fileLength * 1f / MB_SIZE);
            }
            else if (fileLength >= KB_SIZE)
            {
                return string.Format("{0:0.00}KB", fileLength * 1f / KB_SIZE);
            }else
            {
                return string.Format("{0:0.00}B", fileLength * 1f );
            }
        }
    }

}
